package com.projects.android.bus;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONStringer;

import android.util.Log;

import com.projects.android.dto.OrdersDTO;
import com.projects.android.utils.Utils;

public class OrdersBUS {
	public static ArrayList<OrdersDTO> selectByIDUsers(String idUsers) {
		ArrayList<OrdersDTO> myData = null;
		try {
			Map<String, String> mapUser = new HashMap<String, String>();
			mapUser.put("idUsers", idUsers);
			String url = Utils.buildURL(Utils.SERVICE_URI
					+ "/Orders_SelectByIDUsers", mapUser);
			
			// Send GET request to <service>/GetPlates
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet request = new HttpGet(new URI(url));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			HttpResponse response = httpClient.execute(request);
			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("OrdersBUS selectByIDUsers", "Response: " + result);

			if (result != "") {
				// Init array objects
				JSONArray arrData = new JSONArray(result);
				myData = new ArrayList<OrdersDTO>();
				// Loop for all reveived objects
				for (int i = 0; i < arrData.length(); i++) {
					JSONObject data = (JSONObject) arrData.get(i);
					OrdersDTO dto = new OrdersDTO();
					dto.initObject(data);
					myData.add(dto);
				}
			}
		} catch (Exception e) {
			Log.e("OrdersBUS selectByIDUsers", e.toString());
		}
		return myData;
	}
	
	public static int insert(OrdersDTO data) {
		try {
			// POST request to <service>/SaveVehicle
			HttpPost request = new HttpPost(new URI(Utils.SERVICE_URI
					+ "/Orders_Insert"));
			request.setHeader("Accept", "application/json");
			request.setHeader("Content-type", "application/json");

			// Build JSON string
			JSONStringer input = new JSONStringer()
				.object()
					.key("obj")
						.object()
							.key("IDUsers").value(data.getIDUsers())
							.key("IDRestaurant").value(data.getIDRestaurant())
							.key("IsVerified").value(false)
							.key("IsShipped").value(false)
							.key("OrderDate").value(data.getOrderDate())
							.key("DeliveryNote").value(data.getDeliveryNote())
							.key("ReceiverAddress").value(data.getReceiverAddress())
							.key("ReceiverName").value(data.getReceiverName())
							.key("ReceiverPhone").value(data.getReceiverPhone())
							.endObject()
						.endObject();
			
			StringEntity entity = new StringEntity(input.toString());
			request.setEntity(entity);

			// Send request to WCF service
			DefaultHttpClient httpClient = new DefaultHttpClient();
			HttpResponse response = httpClient.execute(request);

			HttpEntity responseEntity = response.getEntity();
			// Read response data into buffer
			String result = Utils.getResponse(responseEntity);
			Log.i("OrdersBUS insert", "Response: " + result);
			Log.i("OrdersBUS insert", "Saving : "
					+ response.getStatusLine().getStatusCode());
			
			return Integer.parseInt(result);

		} catch (Exception e) {
			Log.e("insertData", e.toString());
			return -1;
		}
	}
}
